package com.jluzh10.learning.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jluzh10.learning.dao.CourseMapper;
import com.jluzh10.learning.dao.UsersCourseMapper;
import com.jluzh10.learning.domain.*;
import com.jluzh10.learning.service.CourseService;
import com.jluzh10.learning.service.TeacherService;
import com.jluzh10.learning.utils.PinYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yzg
 * @create 2019/5/24
 */
@Service
@Transactional
public class CourseServiceImpl implements CourseService {
    private final CourseMapper courseMapper;
    private final UsersCourseMapper usersCourseMapper;
    private final TeacherService teacherService;

    @Autowired
    public CourseServiceImpl(CourseMapper courseMapper, UsersCourseMapper usersCourseMapper, TeacherService teacherService) {
        this.courseMapper = courseMapper;
        this.usersCourseMapper = usersCourseMapper;
        this.teacherService = teacherService;
    }

    @Override
    public int addCourse(Course course) throws Exception {
        //设置课程创建时间
        course.setCreateTime(new Date());
        //设置名称拼音
        course.setNamePinyin(PinYinUtil.getPinYin(course.getName()));
        //设置课程类拼音
        course.setClassifyPinyin(PinYinUtil.getPinYin(course.getClassifyPinyin()));
        return courseMapper.insertSelective(course);
    }

    @Override
    public Course selectOne(Integer id) throws Exception {
        Course course = courseMapper.selectByPrimaryKey(id);
        if (course!=null){
            //查询课程教学方信息
            Teacher teacher = teacherService.selectOneById(course.getTeacherId());
            course.setTeacher(teacher);
        }
        return course;
    }

    @Override
    public boolean isCollect(Integer userId, Integer courseId) throws Exception {
        UsersCourseExample example = new UsersCourseExample();
        example.createCriteria().andCourseIdEqualTo(courseId).andUserIdEqualTo(userId);
        List<UsersCourse> usersCourses = usersCourseMapper.selectByExample(example);
        return usersCourses.size() > 0;
    }

    @Override
    public void updateByIdSelective(Course course) throws Exception {
        courseMapper.updateByPrimaryKeySelective(course);
    }

    @Override
    public List<Course> list(Integer size, Integer pageNum, Course course,
                             String orderByClause, String key) throws Exception {
        CourseExample courseExample = new CourseExample();
        CourseExample.Criteria criteria = courseExample.createCriteria();
        if (orderByClause != null) {
            courseExample.setOrderByClause(orderByClause + " desc");
        }
        if (key != null) {//搜索字符串不为空
            criteria.andNameLike("%" + key + "%");
        }
        if (course != null) {//查询条件不为空
            if (course.getLevel() != null && course.getLevel() != 0) {
                //按难度等级筛选
                criteria.andLevelEqualTo(course.getLevel());
            }
            if (course.getClassifyId() != null && course.getClassifyId() != 0) {
                //按课程分类筛选
                criteria.andClassifyIdEqualTo(course.getClassifyId());
            }
            if (course.getGroupId() != null && course.getGroupId() != 0) {
                //按课程方向筛选
                criteria.andGroupIdEqualTo(course.getGroupId());
            }
        }
        if (size == null) {
            size = 15;
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, size);
        return courseMapper.selectByExample(courseExample);
    }

    /**
     * 根据评论更新课程评价信息
     * @param comment 评论
     * @return
     */
    @Override
    public int updateScopeByComment(Comment comment) {
        return courseMapper.updateScopeById(comment);
    }


}
